package net.tngou.tpage.action.api;

import java.io.IOException;
import javax.servlet.ServletException;
import net.tngou.tpage.action.BaseAction;
import net.tngou.tpage.jdbc.OrderType;
import net.tngou.tpage.jdbc.PageUtil;
import net.tngou.tpage.pojo.Attribute;
import net.tngou.tpage.util.StringUtil;

/**
 * 属性操作
 * @author tngou@tngou.net
 *
 */
public class AttributeApi extends BaseAction {

	/**
	 * 列表
	 */
	@Override
	public void execute() throws ServletException, IOException {
		if(user==null){
			run_nologin();return;
		}
		PageUtil page = service.getPage(ask.getPage(), ask.getRows(),"seq",OrderType.ASC, Attribute.class);
		printJson(toJsonPage(page));
	}
	
	/**
	 * 添加属性
	 */
	public void add() {
		if(user==null||user.getRole()!=1){
			run_nologin();return;
		}
		Attribute bean=getAsk(Attribute.class);
		if(isEmpty(bean.getName(),bean.getText())){
			run_false("请求参数不正确！");return;
		}
		long b =bean.save();
		if(b>0){	
			run_success("添加成功！",b);
		}else {run_false("添加失败！");return;}
	}
	
	/**
	 * 编辑属性
	 */
	public void update() {
		if(user==null||user.getRole()!=1){
			run_nologin();return;
		}
		Attribute bean=getAsk(Attribute.class);
		if(ask.getId()==0)
		{
			run_false("请求参数不正确！");return;
		}
		Attribute attribute = bean.get(ask.getId());
		if(attribute==null)
		{
			run_false("请求数据不正确！");return;
		}
		if(StringUtil.isNotEmpty(bean.getName()))attribute.setName(bean.getName());
		if(StringUtil.isNotEmpty(bean.getText()))attribute.setText(bean.getText());
		if(StringUtil.isNotEmpty(bean.getUri()))attribute.setUri(bean.getUri());
		if(StringUtil.isNotEmpty(request.getParameter("seq")))attribute.setSeq(bean.getSeq());
		long b = attribute.update();
		if(b>0){	
			run_success("修改成功！");
		}else {run_false("修改失败！");return;}
	}
	
}
